using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using CRM.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;

namespace CRM.Authorization;

public class UserClaimsPrincipalFactory : UserClaimsPrincipalFactory<User, CRM.Models.Role>
{
    private readonly IHttpContextAccessor _httpContextAccessor;

    public UserClaimsPrincipalFactory(IHttpContextAccessor httpContextAccessor,UserManager<User> userManager, RoleManager<Models.Role> roleManager, IOptions<IdentityOptions> options) : base(userManager, roleManager, options)
    {
        _httpContextAccessor = httpContextAccessor;
    }

    protected override async Task<ClaimsIdentity> GenerateClaimsAsync(User user)
    {
        var id = await base.GenerateClaimsAsync(user);
        // if (_httpContextAccessor?.HttpContext?.User is { } u)
        // {
        //     var loginStamp = u.Identities.FirstOrDefault()?.Claims
        //         .FirstOrDefault(c => c.Type == "LoginStamp");
        //     if (loginStamp != null)
        //         id.AddClaim(loginStamp);
        // }
        return id;
    }
}